package com.colin.app.activity.coordinator;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

import com.colin.app.R;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.app.fragment.example.ExampleFragment;
import com.colin.library.adapter.MyFragmentPagerAdapter;
import com.colin.library.base.BaseFragment;
import com.colin.library.other.Constants;
import com.colin.library.view.progress.CoordinatorTabLayout.CoordinatorTabLayout;

import java.util.List;

public class CoordinatorTabLayoutActivity extends AppActivity implements CoordinatorTabLayoutContract.CoordinatorTabLayoutView, BaseFragment.OnFragmentListener {
    private CoordinatorTabLayoutContract.CoordinatorTabLayoutPresenter mCoordinatorTabLayoutPresenter = null;

    private CoordinatorTabLayout coordinatorTabLayout;
    private ViewPager pager_coordinator_show;
    private MyFragmentPagerAdapter adapter = null;
    @Override
    protected void onDestroy() {
        adapter = null;
        if (null != mCoordinatorTabLayoutPresenter) {
            mCoordinatorTabLayoutPresenter.destroy();
            mCoordinatorTabLayoutPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initPresenter() {
        mCoordinatorTabLayoutPresenter = new CoordinatorTabLayoutPresenterImpl(this, this);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_coordinator_tab_layout;
    }

    @Override
    public void initView() {
        this.pager_coordinator_show = (ViewPager) this.findViewById(R.id.pager_coordinator_show);
        this.coordinatorTabLayout = (CoordinatorTabLayout) this.findViewById(R.id.app_activity_id);
    }

    @Override
    public void initData() {
        mCoordinatorTabLayoutPresenter.initViewPager();
        mCoordinatorTabLayoutPresenter.initCoordinatorTabLayout();
    }

    @Override
    public void initViewPager(List<ExampleFragment> fragmentList, List<Integer> fragmentIDList, List<String> fragmentTitleList) {
        if (adapter == null) {
            adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList, fragmentIDList, fragmentTitleList);
        }
        pager_coordinator_show.setAdapter(adapter);
        pager_coordinator_show.setOffscreenPageLimit(fragmentList.size());
    }

    @Override
    public void initCoordinatorTabLayout(int[] imageHead, int[] colorArray) {
        coordinatorTabLayout.setTitle("实例")
                .setBackEnable(true)
                .setImageArray(imageHead, colorArray)
                .setupWithViewPager(pager_coordinator_show);
    }

    @Override
    public void onFragmentClick(int fragment_id, boolean isOK, int position, Object object) {
        if (isOK) {
            DemoBean demoBean = (DemoBean) object;
            mCoordinatorTabLayoutPresenter.clickItem(demoBean);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_navigation, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        int layoutType = Constants.FRAGMENT_RECYCLER_VIEW_TYPE_LIST;
        switch (item.getItemId()) {
//            case R.id.navigation_item_list:
//                break;
            case R.id.navigation_item_grid:
                layoutType = Constants.FRAGMENT_RECYCLER_VIEW_TYPE_GRID;
                break;
            case R.id.navigation_item_staggered:
                layoutType = Constants.FRAGMENT_RECYCLER_VIEW_TYPE_STAGGERED;
                break;
            default:
                layoutType = Constants.FRAGMENT_RECYCLER_VIEW_TYPE_LIST;
                break;
        }
        mCoordinatorTabLayoutPresenter.setLayoutType(layoutType);
        item.setChecked(true);
        toastShow(item.getTitle().toString());
        return super.onOptionsItemSelected(item);
    }


}
